package com.kaifamiao.thread.d0927;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class Demo01 {

    public static void main(String[] args) {
        // 容量为 1
        ArrayBlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(1);

        try {
            blockingQueue.add(1);
            System.out.println("存放了一个元素");

//            blockingQueue.add(2); 抛出异常，队列满了
//            boolean offer = blockingQueue.offer(2); 返回 false 表示没有添加进去
//            blockingQueue.put(2);  会阻塞，线程一直等待
//            boolean offer = blockingQueue.offer(2, 1, TimeUnit.SECONDS); 阻塞，等待一定时间

            Integer take = blockingQueue.take();
            System.out.println("删除了一个元素 " + take);


        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
